package com.demo.myapplication.ui;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

public class CanvasCircle extends View {

  private int mCircleXY;
  private float mRadius;
  private Paint mCirclePaint;
  private Paint mTextPaint;
  private String mShowText;
  private float mSweepValue = 25;
  private RectF mArcRectF;
  private float mSweepAngle;
  private Paint mArcPaint;
  private float mShowTextSize;
  private String Tag = CanvasCircle.class.getSimpleName();

  public CanvasCircle(Context context) {
    super(context);
  }

  public CanvasCircle(Context context, AttributeSet attrs) {
    super(context, attrs);
  }

  @Override
  protected void onSizeChanged(int w, int h, int oldw, int oldh) {
    super.onSizeChanged(w, h, oldw, oldh);
    initView();
  }

  private void initView() {
    int length = Math.min(getMeasuredHeight(), getMeasuredWidth());

    Log.d(Tag, "length=" + length);
    mCircleXY = length / 2;// 确定圆心坐标
    mRadius = (float) (length * 0.5 / 2);// 确定半径
    mCirclePaint = new Paint();
    mCirclePaint.setAntiAlias(true);// 去锯齿
    mCirclePaint.setColor(getResources().getColor(android.R.color.holo_green_dark));

    // 弧线
    // 矩形
    mArcRectF = new RectF((float) (length * 0.1), (float) (length * 0.1), (float) (length * 0.9),
        (float) (length * 0.9));
    mSweepAngle = (mSweepValue / 100f) * 360f;
    mArcPaint = new Paint();
    mArcPaint.setColor(getResources().getColor(android.R.color.holo_blue_bright));
    mArcPaint.setStrokeWidth((float) (length * 0.1));//圆弧宽度
    mArcPaint.setStyle(Paint.Style.STROKE);//圆弧
    // 文字
    mShowText = setShowText();
    mShowTextSize = setShowTextSize();
    mTextPaint = new Paint();
    mTextPaint.setTextSize(mShowTextSize);
    mTextPaint.setTextAlign(Paint.Align.CENTER);

  }

  private float setShowTextSize() {
    this.invalidate();
    return 15;
  }

  private String setShowText() {
    this.invalidate();
    return "Android Skill";
  }

  public void forceInvalidate() {
    this.invalidate();
  }

  public void setSweepValue(float sweepValue) {
    if (sweepValue != 0) {
      mSweepValue = sweepValue;
    } else {
      mSweepValue = 25;
    }
    this.invalidate();
  }

  public CanvasCircle(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    initView();
  }

  @Override
  protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    setMeasuredDimension(measureSize(widthMeasureSpec), measureSize(heightMeasureSpec));
  }

  private int measureSize(int heightMeasureSpec) {
    int size = 0;
    int minSize = 150;
    int specMode = MeasureSpec.getMode(heightMeasureSpec);
    int specSize = MeasureSpec.getSize(heightMeasureSpec);
    switch (specMode) {
      case MeasureSpec.EXACTLY:
        size = specSize;
        break;
      case MeasureSpec.AT_MOST:
        size = Math.min(minSize, specSize);
        break;
      case MeasureSpec.UNSPECIFIED:
        size = minSize;
        break;
    }
    return size;
  }

  @Override
  protected void onDraw(Canvas canvas) {
    // 绘制圆
    canvas.drawCircle(mCircleXY, mCircleXY, mRadius, mCirclePaint);
    // 绘制圆弧，逆时针绘制，角度跟
    canvas.drawArc(mArcRectF, 90, mSweepAngle, false, mArcPaint);
    // 绘制文字
    canvas.drawText(mShowText, 0, mShowText.length(), mCircleXY, mCircleXY + mShowTextSize / 4, mTextPaint);
  }
}
